#include"jpg.h"

int main(int argc, char const *argv[])
{
    if(argc != 2){
        perror("参数错误");
        return 1;
    }
    int lcd = open("/dev/fb0", O_RDWR);
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    ioctl(lcd, FBIOGET_VSCREENINFO, &vinfo);//可变（Variable）屏幕信息,参数不熟悉
    ioctl(lcd, FBIOGET_FSCREENINFO, &finfo);//固定（Fixed）屏幕信息
    //定义lcd屏可见区数据
    int lcd_w = vinfo.xres;
    int lcd_h = vinfo.yres;//易错点！！！
    int component = vinfo.bits_per_pixel / 8;//每个像素的分量数
    int screen_size = lcd_w * component * lcd_h;
    //映射内存
    char *fb_mem = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, lcd, 0);

    //读取jpeg格式转化为rgb格式
    struct jpeg_info *jinfo = jpg2rgb(argv[1]);

    for(int i = 0; i < lcd_h && i < jinfo->height; i++){
        int lcd_offset = lcd_w * component;
        int rgb_offset = jinfo->width * jinfo->components;
        for(int j = 0; j < lcd_w && j < jinfo->width; j++){
            memcpy(fb_mem + j * component + i * lcd_offset + 0, jinfo->rgb_data + j * jinfo->components + i * rgb_offset + 2, 1);
            memcpy(fb_mem + j * component + i * lcd_offset + 1, jinfo->rgb_data + j * jinfo->components + i * rgb_offset + 1, 1);
            memcpy(fb_mem + j * component + i * lcd_offset + 2, jinfo->rgb_data + j * jinfo->components + i * rgb_offset + 0, 1);
        }
    }

    //释放相关资源
    munmap(fb_mem, screen_size);
    close(lcd);
    return 0;
}
